package com.guigu.qingcheng.controller;

import com.guigu.qingcheng.service.AdService;
import com.guigu.qingcheng.service.CartFegin;
import com.guigu.qingcheng.service.CategoryService;
import com.qingcheng.entity.Result;
import com.qingcheng.pojo.business.Ad;
import com.qingcheng.util.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import com.guigu.qingcheng.service.LocalService;
import com.qingcheng.pojo.goods.TbCategory;
import com.qingcheng.pojo.goods.TbSpu;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;


import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.GET;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class IndexController {

    @Autowired
    private AdService adService;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private LocalService localService;

    @Autowired
    private CartFegin cartFegin;

    @RequestMapping({"/","/index"})
    public String Index(Model model, HttpServletRequest request){
        String username = JwtUtils.getMemberIdByJwtToken(request);
        System.out.println(username);
        // 广告轮播
        Result ad= adService.findByPosiPtion("web_index_lb");
        List<Ad> list= (List<Ad>) ad.getData();

        // 首页列表
        List<Map> category= (List<Map>) categoryService.ReturnMenus().getData();

        // 今日推荐
        List<Map> today=categoryService.Today();

        // 热卖商品显示
        List<Map> bestgoods=categoryService.Best();

        // 最新商品显示
        List<Map> newproduct=categoryService.newproduct();

        // 猜你喜欢
        List<Map> like=categoryService.like();

        // 首页家用电器列表
        HashMap<TbCategory, List<TbSpu>> appliances = localService.appliances("89");

        /********************************************************/

        // 列表显示
        model.addAttribute("category",category);

        // 广告显示
        model.addAttribute("list",list);

        // 今日推荐
        model.addAttribute("today",today);

        // 热卖显示
        model.addAttribute("bestgoods",bestgoods);

        // 最新商品显示
        model.addAttribute("newproduct",newproduct);

        // 首页的推荐喜欢
        model.addAttribute("like",like);

        // 家用电器列表加图片显示
        model.addAttribute("appliances",appliances);

        return "index";
    }

    @GetMapping("/register")
    public String register(){
        return "register";
    }

    @GetMapping("/cart")
    public String cart(String token,Model model){
        Map map = new HashMap();
        map.put("token",token);
        model.addAttribute(map);
        return "cart";
    }

    @GetMapping("/orderUI")
    public String orderUI(){
        return "order";
    }

    @RequestMapping(value = "/pay")
    public String payUI(String ordersn,Integer money){
        return "redirect:http://localhost:10001/protal/showPay?ordersn="+ordersn+"&money="+money+"";
    }

    @GetMapping("/showPay")
    public String showPay(){
        return "pay";
    }

    @GetMapping("/weixinpayUI")
    public String weixinpayUI(String orderId){
        return "redirect:http://localhost:10001/protal/showWeixinpay?orderId="+orderId+"";
    }

    @GetMapping("/showWeixinpay")
    public String showWeixinpay(){
        return "weixinpay";
    }


    @GetMapping("/successUI")
    public String successUI(){
        return "order-success";
    }


    @GetMapping("/orderFailUI")
    public String orderFailUI(){
        return "order-fail";
    }

    @GetMapping("/payfailUI")
    public String payfailUI(){
        return "payfail";
    }

    @GetMapping("/teststompUI")
    public String teststompUI(){
        return "teststomp";
    }

    @GetMapping("/paysuccessUI")
    public String paysuccessUI(String money){
        return "redirect:http://localhost:10001/protal/paySuccess?money="+money+"";
    }

    @GetMapping("/paySuccess")
    public String paySuccess(){
        return "paysuccess";
    }

    @GetMapping("/centerOrderIndex")
    public String centerOrderIndex(){
        return "center-index";
    }

    @GetMapping("/buy")
    @ResponseBody
    public String buyUI(String skuId, Integer num,HttpServletRequest request){
        String username = "Patrick";
        cartFegin.addItem(skuId,num,username);
        return "";
    }

    @GetMapping("/buy2")
    public String buyUI2(String skuId, Integer num){
        return "redirect:http://localhost:10001/protal/buyUI?skuId="+skuId+"&num="+num+"";
    }

    @GetMapping("/buyUI")
    public String buyUI(){
        return "cart";
    }

}
